﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

namespace ABELGX
{
    public class InfoDriver:CarInfo
    {
        public Sensor MoveSpeed { get; set; }
        public Sensor RotationSpeed { get; set; }
        public Sensor DistanceLeft {  get; set; }
        public Sensor DistanceRight { get; set; }
        public List<DriverItem> Items { get; set; } = new List<DriverItem>();
        public InfoDriver(int count, params byte[] func) : base(func)
        {
            MoveSpeed = new Sensor(new SensorSet("运动速度", "m/s", 0.01, 0, "f2"));
            RotationSpeed = new Sensor(new SensorSet("转向速度", "rad/s", 0.01, 0, "f2"));
            DistanceLeft = new Sensor(new SensorSet("左轮里程", "m", 0.0001, 0, "f2"));
            DistanceRight = new Sensor(new SensorSet("右轮里程", "m", 0.0001, 0, "f2"));
            while (Items.Count < count)
            {
                Items.Add(new DriverItem($"驱动器{Items.Count + 1}"));
            }
        }
        public override void ProcessData(byte[] data, int start)
        {
            int i = start + 1;
            switch (data[start])
            {
                case INFO_DRIVER_STATUS:
                    for (int j = 0; j < Items.Count; j++, i++)
                    {
                        Items[j].SetInfo(data[i]);
                    }
                    break;
                case INFO_DRIVER_DATA:
                    MoveSpeed.Value = BitConverter.ToInt16(data, i);
                    i += 2;
                    RotationSpeed.Value = BitConverter.ToInt16(data, i);
                    i += 2;
                    for (int j = 0; j < Items.Count; j++, i += 2)
                    {
                        Items[j].Speed.Value = BitConverter.ToInt16(data, i);
                    }
                    DistanceLeft.Value = BitConverter.ToInt32(data, i);
                    i += 4;
                    DistanceRight.Value = BitConverter.ToInt32(data, i);
                    break;
                default:
                    break;
            }
        }

        public override void InitData()
        {
            MoveSpeed.Value = 0;
            RotationSpeed.Value = 0;
            DistanceLeft.Value = 0;
            DistanceRight.Value = 0;
            foreach (var item in Items)
            {
                item.Speed.Value = 0;
            }
        }
    }
}
